<?php
/**
 * Created by PhpStorm.
 * User: lengbin
 * Date: 2018/6/4
 * Time: 下午2:55
 */

namespace common\core\validators;


use yii\validators\Validator;

class PasswordValidator  extends Validator
{
    /**
     * @var string the regular expression used to validate the attribute value.
     * @see http://www.regular-expressions.info/email.html
     */
    public $pattern = '/^(?=.*[a-zA-Z0-9].*)(?=.*[a-zA-Z\\W].*)(?=.*[0-9\\W].*).{6,20}$/';

    /**
     * @inheritdoc
     */
    public function init()
    {
        parent::init();
        if ($this->message === null) {
            $this->message = \Yii::t('yii', '{attribute}格式错误,由字母和数字组合6-20位.');
        }
    }

    /**
     * @inheritdoc
     */
    protected function validateValue($value)
    {
        if (!is_string($value)) {
            $valid = false;
        } else {
            $valid = preg_match($this->pattern, $value);
        }

        return $valid ? null : [$this->message, []];
    }
}